<?php
if(!defined('PUBLIC_DIR')) die('No Access!');

class Response implements iResponse{
	private $ContentType = null;
	private $StatusCode = null;
	private $HttpHeader = array();
	
	private $Title = null;
	private $HttpMeta = array();
	private $Meta = array();
	
	private $StyleSheet = array();
	private $JavaScript = array();
	
	private $Content = null;
	private $View = null;

	function __construct(){
	
	}
	
	public function SetStatusCode($code, $info){
		$this->ContentType = $code . ' ' . $info;
	}
	public function SetHttpHeader($name, $value, $override = true){
		if($override){
			$this->HttpHeader[$name] = array($value);
		}else{
			$this->HttpHeader[$name][] = $value;
		}
	}
	public function SetContentType($type){
		$this->ContentType = $type;
	}

	public function AddHttpMeta($equiv, $content, $override = false){
		if($override){
			$this->HttpMeta[$equiv] = array($content);
		}else{
			$this->HttpMeta[$equiv][] = $content;
		}
	}
	public function AddMeta($name, $content, $override = false){
		if($override){
			$this->Meta[$name] = array($content);
		}else{
			$this->Meta[$name][] = $content;
		}
	}

	public function SetTitle($title){
		$this->Title = $title;
	}

	public function AddStyleSheet($file, $es = '', $e = array()){}
	public function AddJavaScript($file){
		
	}

	public function SetContent($content){
		$this->View = null;
		$this->Content = $content;
	}
	
	private function RunControler(){

	}
	public function RenderView($type = 'HTML'){
		$view = new View($this->View, $type);
		ob_start();
		$view->Render($this->RunControler());
		$this->Content = ob_get_flush();
	}
	public function SendRespond(){
		//header_remove(); for 5.3.0
		//Set headers
		if(!headers_sent() && $this->StatusCode != null){ header("HTTP/1.1 " . $this->StatusCode); exit();}
		if(!headers_sent() && $this->ContentType != null){ header("Content-type: " . $this->ContentType);}
		if(!headers_sent() && count($this->HttpHeader) > 0)
			foreach($this->HttpHeader as $name => $avalue)
				foreach($avalue as $value)
					header($name . " " . $value);
		//Begin render document
		echo '<html><head>';
		//Add http-equiv metas
		if(count($this->HttpMeta) > 0)
			foreach($this->HttpMeta as $name => $avalue)
				echo '<meta http-equiv="' . $name . '" content="' . implode(', ', $avalue) . '" />';
		//Set title
		if($this->Title != null)
			echo '<title>' . $this->Title . '</title>';
		//Add outher metas
		if(count($this->Meta) > 0)
			foreach($this->Meta as $name => $avalue)
				echo '<meta name="' . $name . '" content="' . implode(', ', $avalue) . '" />';
		//Add style files
		if(count($this->StyleSheet) > 0)
			foreach($this->StyleSheet as $id => $value)
				echo '<link type="text/css" rel="' . $value['type'] . '" href="' . $value['file'] . '" />';
		//Add script files
		if(count($this->JavaScript) > 0)
			foreach($this->JavaScript as $id => $value)
				echo '<script type="text/javascript" charset="' . $value['charset'] . '" src="' . $value['file'] . '"/>';

		echo '</head><body>';
		if($this->Content != null)
			echo $this->Content;
		echo '</body></html>';
	}
}